<?php
namespace Admin\Controller;
use Common\Controller\PublicController;
require './qiniu/autoload.php';
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
use Qiniu\Storage\BucketManager;
class annunciateController extends PublicController
{
	public function  annunciateAction()
	{
		//传入页码
		$currentPage = $_GET['p'];
		$currentPage = $currentPage==NULL?1:$currentPage;
		$pageSize = 10;//每页记录数
		//$totalRow = 0;//所有记录
		//$totalPage = 0;//最大页数
		$start = ($currentPage-1)*$pageSize;
		//$m=M('annunciate');
		
		$totalRow = M('annunciate')->count();
		$totalPage = ceil($totalRow/$pageSize);
		$arr=M('annunciate')->limit($start,$pageSize)->select();
		
		echo $currentPage.','.$totalPage;
		
		$this->assign("currentPage",$currentPage);
		$this->assign("totalPage",$totalPage);
		$this->assign('arr',$arr);		
		$this->display('Annunciate_list');
	}

	public function annunciateAdd(){
		
		// 用于签名的公钥和私钥
		$accessKey = 'dT2-2QWd-zuVY5D5n2p0mV04o8nm7X3dSP5yhBAV';
		$secretKey = 'vqCTCwZ_74c04CAGCCMpWvHlOoce0Q0f28O0P3pk';
		// 初始化签权对象
		$auth = new Auth($accessKey, $secretKey);
		
		$bucket = 'jigou';
		//生成上传token
		$token = $auth->uploadToken($bucket);
		$uploadMgr = new UploadManager();
		$key = date('ymd').time().mt_rand(100,999);
		$filePath = $_FILES['annunciate_p']['tmp_name'];
		//执行上传图片
		$uploadMgr->putFile($token, $key, $filePath);
		
		//$basic = htmlentities($_POST["basic"]);
		$basic = $_POST["basic"];
		unset($_POST["basic"]);
		$_POST["basic"] = $basic;
		//$details = htmlentities($_POST["annunciate_w"]);
		$details = $_POST["annunciate_w"];
		unset($_POST["annunciate_w"]);
		$_POST["annunciate_w"] = $details;
		
		$_POST["annunciate_p"] = $key;
		$_POST['aid'] = $_SESSION['admin_id'];
		$add = M('annunciate')->add($_POST);
		if($add){
            $this->success('添加成功');
        }else{
            $this->error('添加失败');
        }
		
		
		
		
		
		
		
		
		
		
		
		
		/* $m = M('annunciate');
        $data['annunciate_r']=I('post.annunciate_r');
        $data['annunciate_w']=I('post.annunciate_w');
        //$data['time']= date();
        //$data['annunciate_p']=".PUBLIC/Uploads/Images/".$_FILES["annunciate_p"]['name'];
       // $data['annunciate_p']=I('post.annunciate_p');
        //dump($data);
        //$data['annunciate_r']=I('post.annunciate_r');        
        //dump(I('post.'));
        //$m->startTrans();//开启pdo事务，用于删除失败时回滚事件
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728;
        $upload->rootPath = './Public/Uploads/';// 设置附件上传根目录
        $upload->savePath = './Images/';// 设置附件上传根目录
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');
        $upload->saveName = ''; //上传文件的保存名称
        $upload->autoSub = false;//关闭使用子目录保存
        $info = $upload->upload($_FILES);
//dump($data);die;


       if ($info){
            $name = time() . rand(0, 9999);
            $image = new \Think\Image();
            foreach ($info as $k => $v) {
                //dump($v);
                $str = substr(strrchr($v['name'], '.'), 1);//图片格式
                //dump($str);die;
                $name = time() . rand(0, 9999);//命名
                $image->open("./Public/Uploads/Images/{$v['name']}");
                $image->thumb(200, 200)->save("./Public/Uploads/Images/$name" . '.' . $str);//图片格式保持一致
                unlink("./Public/Uploads/Images/{$v['name']}");//删除原名图片
                $data["$k"] = $name . '.' .$info['annunciate_p']['ext'];//图片名写入数组
            }
            
        }
		$data['aid'] = $_SESSION['admin_id'];
		$data['basic'] = $_POST['basic'];
		$data['address'] = $_POST['address'];
		
        $res=$m->add($data);
        if($res){
            $this->success('添加成功');
        }else{
            $this->error('添加失败');
        } */

	}

	
	public function annunciateDelete(){
        $id = I('get.id');
        $m = M('annunciate');
        //dump($arr);die;
        $m->startTrans();//开启pdo事务，用于删除失败时回滚事件
        //删除annunciate内数据
        $arr=$m->where("id=$id")->getField("annunciate_p");//获取图片名称
        unlink("./Public/Uploads/Images/{$arr}");//删除图片
        $deleteAnnunciateDetail = $m->where("id = $id")->delete();//删除当前信息
        if ($deleteAnnunciateDetail) {
            $m->commit();//成功则提交
            $this->success('删除成功');
        } else {
            $m->rollback();//失败则回滚
            //删除失败，回滚数据
            $this->error('删除失败');
        }
    }



	public function Annunciate_detail(){
	    $id = I('get.id');
        $m = M('annunciate');
        $info=$m->find($id);//查询当前id的所有信息
		$this->assign('info',$info);//把获取的ID值传入模板
		$this->display();
	    }


    public function annunciateDetailsAction(){
    	$m = M('annunciate');
        $data['annunciate_r']=I('post.annunciate_r');
        $data['annunciate_w']=I('post.annunciate_w');
        $picname=I('post.annunciate_name');

        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728;
        $upload->rootPath = './Public/Uploads/';// 设置附件上传根目录
        $upload->savePath = './Images/';// 设置附件上传根目录
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');
        $upload->saveName = ''; //上传文件的保存名称
        $upload->autoSub = false;//关闭使用子目录保存
        $info = $upload->upload($_FILES);
//dump($data);die;

       if ($info){
            $name = time() . rand(0, 9999);
            $image = new \Think\Image();
            unlink("./Public/Uploads/Images/{$picname}");
            foreach ($info as $k => $v) {
                //dump($v);
                $str = substr(strrchr($v['name'], '.'), 1);//图片格式
                //dump($str);die;
                $name = time() . rand(0, 9999);//命名
                $image->open("./Public/Uploads/Images/{$v['name']}");
                $image->thumb(200, 200)->save("./Public/Uploads/Images/$name" . '.' . $str);//图片格式保持一致
                unlink("./Public/Uploads/Images/{$v['name']}");//删除原名图片
                $data["$k"] = $name . '.jpg';//图片名写入数组
            }
        //dump($data);
        //echo "0000";
         }
           //dump($data);
         $res=$m->save($data);

          dump($data);
           $arr=$m->getLastSql();//获取最后一次的sql语句
          echo $arr;die;
        if($res){
            $this->success('添加成功');
        }else{
            $this->error('添加失败');
        }
    }

}